package Download;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import Crawler_data.Crawler_data;
import each_class.SongClass;

public class FindMusic {
	public static InputStream findMusic(SongClass songClass) throws IOException { // 寻找歌曲
		if (songClass != null) {
		//	System.out.println("Asdasds");
			if (songClass.getCachingpath() == null && songClass.getDowpath() == null) {
				return Crawler_data.Get_music(songClass);
			} else if (songClass.getDowpath() != null) {
				InputStream in = new FileInputStream(new File(
						songClass.getDowpath() + "\\" + songClass.getSong_name() + songClass.getSong_id() + ".mp3"));
				return in;
			} else {

				InputStream in = new FileInputStream(new File(songClass.getCachingpath() + "\\"
						+ songClass.getSong_name() + songClass.getSong_id() + ".mp3"));
				return in;
			}
		}

		return null;
	}
	

	

	public static InputStream findImage(SongClass songClass) throws Exception {// 寻找图片
		if (songClass != null) {
			if (songClass.getCachingpath() == null && songClass.getDowpath() == null) {
				
					Crawler_data.gravble_image(songClass);
					
					return Crawler_data.Get_image(songClass);
				
			} else if (songClass.getDowpath() != null) {
				InputStream in = new FileInputStream(new File(
						songClass.getDowpath() + "\\" + songClass.getSong_name() + songClass.getSong_id() + ".jpg"));
				return in;
			} else {

				InputStream in = new FileInputStream(new File(songClass.getCachingpath() + "\\"
						+ songClass.getSong_name() + songClass.getSong_id() + ".jpg"));
				return in;
			}
		}
		return null;
	}

	public static Map<String, String> findlyric(SongClass songClass) throws Exception {// 寻找歌词
		if (songClass != null) {
			if (songClass.getCachingpath() == null && songClass.getDowpath() == null) {
				return Crawler_data.gravble_lyric(songClass);
			} else if (songClass.getDowpath() != null) {

				Map<String, String> map = new LinkedHashMap<String, String>();
				FileInputStream fis = new FileInputStream(new File(
						songClass.getDowpath() + "\\" + songClass.getSong_name() + songClass.getSong_id() + ".obj"));
				ObjectInputStream ois = new ObjectInputStream(fis);
				map = (Map<String, String>) ois.readObject();
				ois.close();
				return map;
			} else {
				Map<String, String> map = new LinkedHashMap<String, String>();
				FileInputStream fis = new FileInputStream(new File(songClass.getCachingpath() + "\\"
						+ songClass.getSong_name() + songClass.getSong_id() + ".obj"));
				ObjectInputStream ois = new ObjectInputStream(fis);
				map = (Map<String, String>) ois.readObject();
				ois.close();

				return map;
			}
		}
		return null;
	}

}
